package es.ats.codington.festival.daos;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.FERSDbQuery;


/***    
 * Class EventSignUpDAO
 * This class is the implementation of the IEventSignUpDAO class
 * We implements the methods of the class
 *
 *
 * @author  Borja ventDAPalaz�n
 * @see     DAOS
 * @since   1.0
 */
public class EventSignUpDAOImp implements IEventSignUpDAO {
	
	private FERSDataConnection con;
	private FERSDbQuery query;
	private EventSignUp esu;
	
	
	//Getters and Setters
	

	public FERSDataConnection getCon() {
		return con;
	}

	public EventSignUp getEsu() {
		return esu;
	}

	public void setEsu(EventSignUp esu) {
		this.esu = esu;
	}

	public void setCon(FERSDataConnection con) {
		this.con = con;
	}

	public FERSDbQuery getQuery() {
		return query;
	}

	public void setQuery(FERSDbQuery query) {
		this.query = query;
	}
	
	
	

	//Constructor
	public EventSignUpDAOImp(){
		con=new FERSDataConnection();
		query=new FERSDbQuery();
	}
	
	public EventSignUpDAOImp(EventSignUp esu) {
		super();
		this.esu = esu;
	}
	
	
	

	/**
	 * 
	 * @param esu EventSignUp
	 * @return number of rows affected if register was successful 
	 */
	    //COMPLETO
	@Override
	public int registerVisitorForNewEventDAO(EventSignUp esu) throws DAOException{
	    	try{
	    		
		    	//con.connect();
		    	PreparedStatement pStmt = con.prepareStatement(query.registerVisitorForNewEvent(esu));
		    	pStmt.setInt(1,esu.getIdVisitor());
		    	pStmt.setInt(2,esu.getIdEvent());
		    	System.out.println(pStmt);
				int a=pStmt.executeUpdate();
				//con.disconnect();
				return a;
			} catch (Exception ex) {
				DAOException dex=new DAOException(ex.getMessage(),ex.getCause());
				//con.disconnect();
				throw dex;
				
			}
	}

	/**
	 * 
	 * @param esu EventSignUp
	 * @return number of rows affected if register was successful 
	 */
	
	@Override
	public boolean isRegisteredToEvent(EventSignUp esu)  throws DAOException {
		
		try{
	    	//con.connect();
	    	PreparedStatement pStmt = con.prepareStatement(query.isRegisteredToEvent(esu));
	    	pStmt.setInt(1,esu.getIdVisitor());
	    	pStmt.setInt(2,esu.getIdEvent());
	    	ResultSet rs = pStmt.executeQuery();
	    	
	    	return rs.next();
	    	
			}
		    catch (Exception ex) {
		    	DAOException dex=new DAOException(ex.getMessage(),ex.getCause());
				//con.disconnect();
				throw dex;
	       }
   
	}	
	/**
	 * 
	 * @param esu EventSignUp
	 * @return number of rows affected if unregister was successful
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	
	//COMPLETO
	@Override
	public int unregisterVisitorForEventDAO(EventSignUp esu)  throws DAOException {
		
    	try{
	    	//con.connect();
	    	PreparedStatement pStmt = con.prepareStatement(query.unregisterVisitorForEvent(esu));
	    	pStmt.setInt(1,esu.getIdEvent());
			int a=pStmt.executeUpdate();
			//con.disconnect();
			return a;
		} catch (Exception ex) {
			DAOException dex=new DAOException(ex.getMessage(),ex.getCause());
			//con.disconnect();
			throw dex;
		}
}
	/**
	 * 
	 * @param esu EventSignUp
	 * @return list of event of a event visitor 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	//COMPLETO
	@Override
	public List<Event> viewEventsOfUserDAO(EventSignUp esu) throws DAOException {
		
		   List<Event> a= new ArrayList<Event>();
		
        try{
	    	//con.connect();
	    	PreparedStatement pStmt = con.prepareStatement(query.viewEventsOfUserDAO(esu));
	    	pStmt.setInt(1,esu.getIdVisitor());
	    	pStmt.setInt(2,esu.getIdEvent());
	    	ResultSet rs = pStmt.executeQuery();
			
			while(rs.next()){
				Event e=new Event();
				e.setEventId(Integer.parseInt(rs.getString("eventId")));
				e.setName(rs.getString("name"));
				a.add(e);
			}
			//con.disconnect();
			return a;
		} catch (Exception ex) {
			DAOException dex=new DAOException(ex.getMessage(),ex.getCause());
			//con.disconnect();
			throw dex;
		
		}
	}

	
	/**
	 * 
	 * @param esu EventSignUp
	 * @return int if has been registered with success
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	
	//DUDAAAAAAAAAA
	@Override
	public int updateRestSeatsAvailableEvent(Event esu) throws DAOException {

            try{
	    	//con.connect();
            	PreparedStatement pStmt = con.prepareStatement(query.updateRestSeatsAvailableEvent(esu));
            	pStmt.setInt(1,esu.getSeatsAvailable());
    	    	pStmt.setInt(2,esu.getEventId());
    	    	int a= pStmt.executeUpdate();
			//con.disconnect();
			return a;
		} catch (Exception ex) {
			DAOException dex=new DAOException(ex.getMessage(),ex.getCause());
			//con.disconnect();
			throw dex;
		}	
		
	}
	

	
	

}
